﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Banner_BannerAdd : System.Web.UI.UserControl
{
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        MessAlert1.HideMessage();
        if (!IsPostBack)
        {
            this.SetLableLanguage();
            this.BindDataPriorityDropdown();
            this.BindDataToLanguageDropdown();

            this.SetValidate();
            this.InitData();
        }
    }
    private void SetLableLanguage()
    {
        chkActive.Text = MultipleLanguage.LBL_ACTIVE[nDefaultLanguage - 1];
        chkDisplayOrder.Text = MultipleLanguage.LBL_PRIORITY[nDefaultLanguage - 1];

        btnCancel.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
        lblImage.Text = MultipleLanguage.LBL_IMAGE[nDefaultLanguage - 1];
        lblInitContent.Text = MultipleLanguage.LBL_INIT_CONTENT[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblTitle.Text = MultipleLanguage.LBL_TITLE[nDefaultLanguage - 1];
        lblType.Text = MultipleLanguage.LBL_BANNERTYPE[nDefaultLanguage - 1];

        lblColorCode.Text = MultipleLanguage.LBL_COLORCODE[nDefaultLanguage-1];
        lblColorCodeIniCnt.Text = MultipleLanguage.LBL_COLORCODE[nDefaultLanguage-1];
        lblFontSize.Text = MultipleLanguage.LBL_FONTSIZE[nDefaultLanguage-1];
        lblFontSizeInitialContent.Text = MultipleLanguage.LBL_FONTSIZE[nDefaultLanguage-1];
        lblLink.Text = MultipleLanguage.LBL_LINK[nDefaultLanguage - 1];
        lblStandardSize.Text = MultipleLanguage.LBL_STANDSIZE[nDefaultLanguage-1];
    }
    #region Bind Data Dropdown
    private void BindDataToLanguageDropdown()
    {
        DataTable dtbLanguage = new DataTable();
        Languages objLanguage = new Languages();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();

    }
    private void BindDataPriorityDropdown()
    {
        DataTable dtbBanner = new DataTable();
        Banner objBanner = new Banner();

        dtbBanner = objBanner.GetAll();
        int nCount = dtbBanner.Rows.Count;
        string sAction = GetQuerryString(Config.ACTION);//Lay Thong tin Edit, Insert
        int nRealValue = 0;
        if (sAction == Config.ACTION_EDIT)//Kiem tra dieu kien
        {
            nRealValue = nCount;
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }

            eBanner eBannerEntity = this.BannerEntity();
            string sPriority = ConvertData.ConvertToString(eBannerEntity.Priority);

            ddlPriority.SelectedValue = sPriority;
        }
        else
        {
            nRealValue = nCount + 1;
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }
            ddlPriority.SelectedValue = ConvertData.ConvertToString(nRealValue);
        }
        //
    }
    #endregion

    #region Click Events
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string sGoToPageMan = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER;
        Response.Redirect(sGoToPageMan);
    }
    protected void btnSave_Click1(object sender, EventArgs e)
    {
        int nActionResult = 0;
        int nPriority = ConvertData.ConvertToInt(ddlPriority.SelectedValue);
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        Banner objBannerObj = new Banner();
        objBannerObj.Data = SetData(nCurrentLanguage, nPriority);

        string sAction = GetQuerryString(Config.ACTION);
        if (sAction == Config.ACTION_EDIT)
        {
            //Is Edit
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
            nActionResult = objBannerObj.Update(nItemID);
            if (nActionResult > 0)
            {
                MessAlert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                // Show updated image

                if (objBannerObj.Data.Images.ToString() != "")
                {
                    imgImage.ImageUrl = Config.ROOT + Config.IMAGE_BANNER_UPLOAD + objBannerObj.Data.Images.ToString();
                }
                if (objBannerObj.Data.Images.ToString() == "")
                {
                    objBannerObj.Data.Images = Config.ROOT + "/Images/no_image.gif";
                }
            }
            else
                MessAlert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[1]);
        }
        else
        {
            // Is Insert New

            nActionResult = objBannerObj.Insert();
            if (nActionResult > 0)
            {
                string sURL = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER  + "&" + Config.MESS_ID + "=" + Config.MESSAGE_INSERT;
                Response.Redirect(sURL);
            }

        }
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        InitData();
    }
    #endregion

    #region Relative Functions
    private void SetValidate()
    {
        rfvTitle.ErrorMessage = "Please Input Title.";
        rfvInitContent.ErrorMessage = "Please Input Initial Content.";
    }
    private void InitData()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);

        try
        {
            if (sQueery == Config.ACTION_EDIT)
            {
                int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nItemID > 0)
                    GetData(nCurrentLang, nItemID);

                Session[Config.SESSION_LANGID] = "1";
            }
            else { imgImage.ImageUrl = Config.IMAGE_NO_IMAGE; }
        }
        catch
        {
            string sRedirect = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER  + "&" + Config.MESS_ID + "=" + Config.ITEM_NOTEXIST;
            Response.Redirect(sRedirect);
        }
    }
    private void GetData(int CurrentLang, int ItemID)
    {
        // Get data to view on UI Controls
        int nDisplayOrder = 0;
        int nStatus = 0;
        //chkActive.Enabled = true;

        Banner objBanner = new Banner();
        objBanner.LoadById(ItemID);


        ddlLanguage.SelectedValue = ConvertData.ConvertToString(CurrentLang);

        nDisplayOrder = ConvertData.ConvertToInt(objBanner.Data.Priority);
        if (nDisplayOrder > 0)
            chkDisplayOrder.Checked = true;
        else
            chkDisplayOrder.Checked = false;

        nStatus = ConvertData.ConvertToInt(objBanner.Data.Status);
        if (nStatus > 0)
            chkActive.Checked = true;
        else
            chkActive.Checked = false;

        switch (CurrentLang)
        {
            case 1: // Vietnam
                txtTitle.Text = objBanner.Data.Title1;
                txtInitContent.Text = objBanner.Data.InitContent1;
                break;
            case 2: // English
                txtTitle.Text = objBanner.Data.Title2;
                txtInitContent.Text = objBanner.Data.InitContent2;
                break;

        }

        txtLink.Text = ConvertData.ConvertToString(objBanner.Data.Link);
        ddlType.SelectedValue = ConvertData.ConvertToString(objBanner.Data.BannerType);
        if (objBanner.Data.Images != "")
            imgImage.ImageUrl = Config.ROOT + Config.IMAGE_BANNER_UPLOAD + objBanner.Data.Images;
        else imgImage.ImageUrl = Config.IMAGE_NO_IMAGE;

        txtInitContentColorCode.Text = ConvertData.ConvertToString(objBanner.Data.InitContentColor);
        txtInitContentFontSize.Text = ConvertData.ConvertToString(objBanner.Data.InitContentFontSize);
        txtTitleColorCode.Text = ConvertData.ConvertToString(objBanner.Data.TitleColor);
        txtTitleFontSize.Text = ConvertData.ConvertToString(objBanner.Data.TitleFontSize);

    }
    private eBanner BannerEntity()
    {
        eBanner entityBanner = new eBanner();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            Banner objBanner = new Banner();
            objBanner.LoadById(nItemID);
            entityBanner = objBanner.Data;
        }
        return entityBanner;
    }
    private eBanner SetData(int CurrentLang, int nPriority)
    {
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        eBanner eBannerEntity = this.BannerEntity();
        Banner objBanner = new Banner();

        if (chkDisplayOrder.Checked) objBanner.Data.Priority = ConvertData.ConvertToInt(ddlPriority.SelectedValue);
        else objBanner.Data.Priority = 0;

        objBanner.Data.Link = ConvertData.ConvertToString(txtLink.Text.Trim());

        if (iptImage.Value != "")
        {
            Support.DeleteFile("banner", eBannerEntity.Images);
            objBanner.Data.Images = Support.UploadFile(iptImage, "banner");
        }
        if (iptImage.Value == "")
        {
            if (eBannerEntity.Images != "")
                objBanner.Data.Images = ConvertData.ConvertToString(eBannerEntity.Images);
            else objBanner.Data.Images = "";
        }


        if (chkActive.Checked) objBanner.Data.Status = Config.STATUS_ACTIVE;
        else objBanner.Data.Status = Config.STATUS_INACTIVE;

        objBanner.Data.BannerType = ConvertData.ConvertToInt(ddlType.SelectedValue);

        string sTitle = ConvertData.ConvertToString(ConvertData.RemoveHTMLTag(txtTitle.Text.Trim()));
        string sInitContent = ConvertData.ConvertToString(ConvertData.RemoveHTMLTag(txtInitContent.Text.Trim()));

        switch (CurrentLang)
        {
            case 1:
                objBanner.Data.Title1 = sTitle;
                objBanner.Data.InitContent1 = sInitContent;

                objBanner.Data.Title2 = ConvertData.ConvertToString(eBannerEntity.Title2);
                objBanner.Data.InitContent2 = ConvertData.ConvertToString(eBannerEntity.InitContent2);
                break;
            case 2:
                objBanner.Data.Title2 = sTitle;
                objBanner.Data.InitContent2 = sInitContent;

                objBanner.Data.Title1 = ConvertData.ConvertToString(eBannerEntity.Title1);
                objBanner.Data.InitContent1 = ConvertData.ConvertToString(eBannerEntity.InitContent1);
                break;

        }
        objBanner.Data.InitContentColor = txtInitContentColorCode.Text.Trim();
        objBanner.Data.InitContentFontSize = ConvertData.ConvertToInt(txtInitContentFontSize.Text.Trim());
        objBanner.Data.TitleColor = txtTitleColorCode.Text.Trim();
        objBanner.Data.TitleFontSize = ConvertData.ConvertToInt(txtTitleFontSize.Text.Trim());
        return objBanner.Data;
    }
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    #endregion
}